/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.bohong.freshShop.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.bohong.freshShop.dao.entity.UserEntity;
import com.bohong.freshShop.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author runhong.wyb
 * @version $Id: UserController.java, v 0.1 2024-01-19 16:10 san.zhang Exp $$
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/register")
    public boolean register(@RequestBody UserEntity userEntity) {
        int register = userService.register(userEntity);
        if(register > 0) {
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping("/login")
    public String login(String username, String password) {
        UserEntity userEntity = userService.queryByUsername(username);
        if(StringUtils.equals(userEntity.getUsername(),username) && StringUtils.equals(userEntity.getPassword(),password)) {
            StpUtil.login(userEntity.getUserId());
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    // 测试注销  ---- http://localhost:8081/acc/logout
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }
}
